package invest.utils;

import invest.pojo.datapojo.Wencai;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class WencaiUtil {
    private static WebDriver driver=null;
    static {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-gpu");
        options.addArguments("--disable-blink-features=AutomationControlled");
        options.addArguments("--disable-dev-shm-usage");
        List<Object> list=new ArrayList<>();
        list.add("enable-automation");
        options.setExperimentalOption("excludeSwitches",list);
        driver= new ChromeDriver(options);
        String js1="Object.defineProperties(navigator, {webdriver:{get:()=>undefined}});";
        ((ChromeDriver) driver).executeScript(js1);
    }

    public synchronized static List<Wencai> getAstock(String question) throws UnsupportedEncodingException, InterruptedException {
        WebDriver driver = WencaiUtil.driver;
        String urlString = URLEncoder.encode(question, "utf-8");
        long currenttime = System.currentTimeMillis();
        driver.get("http://search.10jqka.com.cn/unifiedmobile/#/result?question="+urlString+"&queryType=&sign="+currenttime);
        Thread.sleep(3000);
        List<Wencai> wencais=parseData(driver);
        return wencais;
    }
    public synchronized static List<Wencai> getHstock(String question) throws UnsupportedEncodingException, InterruptedException {
        WebDriver driver = WencaiUtil.driver;
        String urlString = URLEncoder.encode(question, "utf-8");
        long currenttime = System.currentTimeMillis();
        driver.get("http://search.10jqka.com.cn/unifiedmobile/#/result?question="+urlString+"&queryType=hkstock&sign="+currenttime);
        Thread.sleep(3000);
        List<Wencai> wencais=parseData(driver);
        return wencais;
    }


    private synchronized static List<Wencai> parseData(WebDriver driver) throws UnsupportedEncodingException, InterruptedException {
        List<WebElement> names=driver.findElements(By.xpath("//span[text()='股票']/ancestor::table//tr//td[2]//span[1]"));
        List<Wencai> wencais = new ArrayList<>();
        for (int i = 0; i <names.size() ; i++) {
            Wencai wencai = new Wencai();
            wencai.setName(names.get(i).getText());
            wencais.add(wencai);
        }
        List<WebElement> codes=driver.findElements(By.xpath("//span[text()='股票']/ancestor::table//tr//td[2]//span[2]"));
        for (int i = 0; i <wencais.size() ; i++) {
            Wencai w = wencais.get(i);
            w.setCode(codes.get(i).getText().substring(0,codes.get(i).getText().length()-3));

        }

        List<WebElement> nowPrices=driver.findElements(By.xpath("//span[contains(text(), '最新价')]/ancestor::table/tr/td[1]/span"));

        for (int i = 0; i <wencais.size() ; i++) {
            Wencai w = wencais.get(i);
            w.setNowPrice(Double.valueOf(nowPrices.get(i).getText()));
        }

        List<WebElement> nowRiseRanges=driver.findElements(By.xpath("//span[contains(text(), '最新价')]/ancestor::table/tr/td[2]/span"));

        for (int i = 0; i <wencais.size() ; i++) {
            Wencai w = wencais.get(i);
            w.setNowRiseRange(Double.valueOf(nowRiseRanges.get(i).getText().substring(0,nowRiseRanges.get(i).getText().length()-1)));

        }
        List<WebElement> timeSharingTurnoverRates=driver.findElements(By.xpath("//span[contains(text(), '换手率(%)')]/ancestor::table/tr/td[3]"));
        for (int i = 0; i <wencais.size() ; i++) {
            Wencai w = wencais.get(i);
            w.setTimeSharingTurnoverRate(Double.valueOf(timeSharingTurnoverRates.get(i).getText().trim()));

        }
        List<WebElement> timeSharingTurnoverRankings=driver.findElements(By.xpath("//span[contains(text(), '换手率排名名次')]/ancestor::table/tr/td[5]"));
        for (int i = 0; i <wencais.size() ; i++) {
            Wencai w = wencais.get(i);
            w.setTimeSharingTurnoverRanking(Integer.valueOf(timeSharingTurnoverRankings.get(i).getText().trim()));
        }
        List<WebElement> closingProfits=driver.findElements(By.xpath("//span[contains(text(), '换手率排名名次')]/ancestor::table/tr/td[7]"));
        for (int i = 0; i <wencais.size() ; i++) {
            Wencai w = wencais.get(i);
            w.setClosingProfit(Double.valueOf(closingProfits.get(i).getText().trim()));

        }
        return wencais;
    }
    public static void driverClose() {
        driver.close();

    }


    public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException {
        String question = "2月15日涨停，股票代码";
        List<Wencai> wencais=getAstock(question);
        long currenttime = System.currentTimeMillis();
        System.out.println(currenttime);

    }
}
